package com.lanren.familybookapp.app;

import com.lanren.common.app.ApiUrl;
import com.lanren.common.utils.SPUtils;
import com.lanren.common.utils.StringUtils;
import com.lanren.easyhttp.model.HttpHeaders;

/**
 * @ClassName AppUserHelper
 * @Description TODO 用户信息持久化工具
 * @Author chongheng.wang
 * @Date 2019/9/3 15:16
 * @Version 1.0
 */
public class AppUserHelper {

    private SPUtils mUtil;
    private String mToken;
    private static final String FILE_NAME = "AppUserHelper";
    private static final String KEY_ONE_START = "oneStart";
    private static final String KEY_APP_INIT = "appInit";
    private static final String KEY_TOKEN = "token";
    private static final String KEY_USER = "loginUser";

    private UserInfoBean userInfoBean;

    /**
     * 使用静态内部内保证单例安全
     */
    private static class SingletonHolder {
        private static final AppUserHelper instance = new AppUserHelper();
    }

    public static AppUserHelper getInstance() {
        return SingletonHolder.instance;
    }

    private AppUserHelper() {
        mUtil = SPUtils.get(FILE_NAME);
    }

    public void setKeyToken(String token) {
        mToken = token;
        mUtil.putString(KEY_TOKEN, token);
    }

    public String getKeyToken() {
        if (StringUtils.isEmpty(mToken)) {
            mToken = mUtil.getString(KEY_TOKEN);
        }
        return mToken;
    }

    public void setKeyOneStart(int oneStart) {
        mUtil.putInt(KEY_ONE_START, oneStart);
    }

    public int getKeyOneStart() {
        return mUtil.getInt(KEY_ONE_START, 0);
    }

    public void setKeyAppInit(int appInit) {
        mUtil.putInt(KEY_APP_INIT, appInit);
    }

    public int getKeyAppInit() {
        return mUtil.getInt(KEY_APP_INIT, 0);
    }

    public void setUserInfoBean(UserInfoBean userInfo) {
        userInfoBean = userInfo;
        mUtil.putObject(KEY_USER, userInfo);
    }

    private UserInfoBean getUserInfoBean() {
        if (userInfoBean == null) {
            userInfoBean = mUtil.getObject(KEY_USER, UserInfoBean.class);
        }
        return userInfoBean;
    }

    /**
     * 返回不为空的用户对象
     *
     * @return
     */
    public UserInfoBean getUser() {
        final UserInfoBean u = getUserInfoBean();
        if (u == null) {
            return new UserInfoBean();
        } else {
            return u;
        }
    }

    /**
     * 是否登录
     *
     * @return true 登陆，false 没有登陆
     */
    public boolean isLogin() {
        return !StringUtils.isEmpty(getKeyToken());
    }

    public void logout() {
        setKeyOneStart(0);
        setUserInfoBean(null);
        setKeyToken(null);
    }

    public static HttpHeaders getHttpHeaders() {
        HttpHeaders headers = new HttpHeaders();
        String token = AppUserHelper.getInstance().getKeyToken();
//          String token = AppUserHelper.getInstance().getKeyToken()+"0";
        headers.put(ApiUrl.HEADER.HEADER_TOKEN, token);
        //设备系统版本
        headers.put(ApiUrl.HEADER.SYSTEMVERSION, ApiUrl.HEADER.VALUE_OS);
        //设备类型
        headers.put(ApiUrl.HEADER.HEADER_DEVICEID, ApiUrl.HEADER.getHeaderDeviceidValue());
        return headers;
    }
}
